package com.xjc.business.controller.colladmin;

import com.xjc.business.entity.po.coladmin.PersonPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.coladmin.PersonVo;
import com.xjc.business.service.ClassAdminsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-01-29 11:28
 */
@RestController
@RequestMapping("/college_admin/person")
public class PersonController {

    @Autowired
    ClassAdminsService classAdminsService;

    /**
     * 分页查询人员信息
     * @param pageSize 每页显示条数
     * @param currentPage  当前页码
     * @param adminUserName 用户名
     * @param adminNickName 昵称
     * @return 信息
     */
    @RequestMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<PersonVo>> getPersonPage(@PathVariable(value = "pageSize",required = false) Integer pageSize, @PathVariable(value = "currentPage",required = false) Integer currentPage, String adminUserName, String adminNickName,  Integer collegeId) {
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }

        if( collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return classAdminsService.getClassAdminPage(pageSize,currentPage,adminUserName,adminNickName,collegeId);

    }

    /**
     * 添加管理员
     * @param personPo 管理员信息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addPerson(@RequestBody(required = false) PersonPo personPo){

        if(personPo == null || personPo.getCollegeId() == null ||  personPo.getUsername() == null || personPo.getPassword() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return classAdminsService.addClassAdmin(personPo);

    }

    /**
     * 获取学院下的管理员列表
     * @param collegeId 学院id
     * @return 信息
     */
    @GetMapping("/list/{collegeId}")
    public Result<List<PersonVo>> getPersonList(@PathVariable(value = "collegeId",required = false) Integer collegeId){
        if(collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return classAdminsService.getPersonList(collegeId);
    }
}
